package pers.lt.admin.services;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pers.lt.admin.dao.OfficesDaoImp;
import pers.lt.hibernatebeans.OfficesInfoEntity;
import pers.lt.tools.StringFacs;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 18621 on 2016/7/5.
 */
@Component("OfficesServicesImp")
@Transactional(readOnly=false)
public class OfficesServicesImp {
    @Resource
    private OfficesDaoImp officesDaoImp;

    public OfficesInfoEntity addOffices(OfficesInfoEntity officesInfoEntity){
        //如果ID为空，说明是新增，给他New一个ID
        if(StringFacs.isEmpty(officesInfoEntity.getId())) officesInfoEntity.setId(StringFacs.getGuid32());
        OfficesInfoEntity a = officesDaoImp.addOffices(officesInfoEntity);
        return a;
    }
    public OfficesInfoEntity selectOffices(OfficesInfoEntity officesInfoEntity){
        OfficesInfoEntity a = officesDaoImp.selectOffices(officesInfoEntity);
        return a;
    }
    public List<Map> officesList(OfficesInfoEntity officesInfoEntity){
        List <Map> rList = new ArrayList<Map>();
        List <Map> list = officesDaoImp.officesList(officesInfoEntity);

        for(int i=0 ;i<list.size();i++){
            Map map = list.get(i);
            int childCount  = 0;
            if(!StringFacs.isEmpty(map.get("CHILD"))) childCount = Integer.parseInt(map.get("CHILD").toString());
            if(childCount>0){
                Map mapT = new HashMap();
                mapT.put("ID",map.get("ID")+"_1");
                mapT.put("OFFICES_NAME","Ttest");
                List a = new ArrayList<Map>();
                a.add(mapT);
                map.put("children",a);
                map.put("state","closed");
            }else map.put("children",new ArrayList<Map>());
            rList.add(map);
        }
        return rList;
    }
    //组织COMBOXTREE数据格式的树
    public List<Map> officesListTree(OfficesInfoEntity officesInfoEntity){
        List <Map> rList = new ArrayList<Map>();
        List <Map> list = officesDaoImp.officesList(officesInfoEntity);
        //第一条数据，请选择单位 默认为无值,只有当请求是第一次父节点才需要返回   ，后面取各个父下面的子的时候不需要再返回了
        if(StringFacs.isEmpty(officesInfoEntity.getPid())) {
            Map firstMap = new HashMap();
            firstMap.put("id", "");
            firstMap.put("text", "请选择单位");
            rList.add(firstMap);
        }
        //要把 ‘list’ Map中的  ID 和 OFFICES_NAME 两个字段改变成id ,text 因为easyui comomtree组件只认这个格式 结果放到rList中
        for(int i=0 ;i<list.size();i++){
            Map map = list.get(i);
            Map rMap = new HashMap();
            rMap.put("id",map.get("ID"));
            rMap.put("text",map.get("OFFICES_NAME"));
            int childCount  = 0;
            if(!StringFacs.isEmpty(map.get("CHILD"))) childCount = Integer.parseInt(map.get("CHILD").toString());
            if(childCount>0){
//                Map mapT = new HashMap();
//                mapT.put("id",map.get("ID")+"_1");
//                mapT.put("text","Ttest");
//                List a = new ArrayList<Map>();
//                a.add(mapT);
//                rMap.put("children",a);
                rMap.put("state","closed");
            }
//            else rMap.put("children",new ArrayList<Map>());
            rList.add(rMap);
        }
        return rList;
    }
    public void officesDelete(OfficesInfoEntity officesInfoEntity){
        String [] ary= officesInfoEntity.getId().split("~");
        for(String str:ary){
            OfficesInfoEntity officesInfoEntity1 = new OfficesInfoEntity();
            officesInfoEntity1.setId(str);
            if(!StringFacs.isEmpty(str))officesDaoImp.officesDelete(officesDaoImp.selectOffices(officesInfoEntity1));
        }
    }
}
